﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EBSystem.Enum;
using EBSystem.SQL;
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace EBSystem.ViewModels
{
    class RegisterViewModel : ObservableObject
    {
        private string? userNameInput;
        public string? UserNameInput
        {
            get => userNameInput;
            set
            {
                userNameInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private string? userPasswordInput;
        public string? UserPasswordInput
        {
            get => userPasswordInput;
            set
            {
                userPasswordInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private string? userNickNameInput;
        public string? UserNickNameInput
        {
            get => userNickNameInput;
            set
            {
                userNickNameInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private string? userEmailInput;
        public string? UserEmailInput
        {
            get => userEmailInput;
            set
            {
                userEmailInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private string? userPhoneNumberInput;
        public string? UserPhoneNumberInput
        {
            get => userPhoneNumberInput;
            set
            {
                userPhoneNumberInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private bool registerIsEnabled = false;
        public bool RegisterIsEnabled
        {
            get => registerIsEnabled;
            set
            {
                registerIsEnabled = value;
                OnPropertyChanged();
            }
        }

        public void InputCheck()
        {
            if (UserNameInput is null || UserNameInput == "" ||
                UserPasswordInput is null || UserPasswordInput == "" ||
                UserNickNameInput is null || UserNickNameInput == "" ||
                UserEmailInput is null || UserEmailInput == "" ||
                UserPhoneNumberInput is null || UserPhoneNumberInput == "")
            {
                RegisterIsEnabled = false;
                return;
            }
            RegisterIsEnabled = true;
        }

        public bool Register(AccountTypeEnum accountType)
        {
            bool success = accountType switch
            {
                AccountTypeEnum.User => SQLConnection.CreateCustomerAccount(
                        UserNameInput,
                        UserPasswordInput,
                        UserNickNameInput,
                        UserEmailInput,
                        UserPhoneNumberInput
                    ),
                AccountTypeEnum.Business => SQLConnection.CreateBusinessAccount(
                        UserNameInput,
                        UserPasswordInput,
                        UserNickNameInput,
                        UserEmailInput,
                        UserPhoneNumberInput
                    ),
                AccountTypeEnum.Administrator => false,
                _ => throw new NotImplementedException()
            };

            return success;
        }
    }
}